// #ifdef H5
import JsBridge from 'dsbridge';
let nativeApi: any = {
    /**
     * 设置 沉浸式
     * @param immersive: 0: 不设置 1: 设置·
     * @returns 
     */
    setImmersive: (immersive: string) => {
        if (JsBridge.hasNativeMethod('setImmersive')) {
            JsBridge.call('setImmersive', immersive);
        }
    },
    /**
     * 设置状态栏颜色
     * @param color :'dark' | 'light'
     */
    setStatusBarTxtColor: (color: string) => {
        if (JsBridge.hasNativeMethod('setStatusBarTxtColor')) {
            JsBridge.call('setStatusBarTxtColor', color);
        }
    },
    /**
 * 设置状态栏颜色
 * @param color :'0' | '1'
 */
    showLoading: (show: string) => {
        if (JsBridge.hasNativeMethod('showProgress')) {
            JsBridge.call('showProgress', show);
        }
    },
    /**
     * 定位
     * @returns 
     */
    getLocation: () => {
        if (!JsBridge.hasNativeMethod('getLocation')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('getLocation', '', (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res);
                } else {
                    reject(res);
                }
            });
        });

    },
    // 调用原生，打开应用设置
    go2Settings: () => {
        if (JsBridge.hasNativeMethod('go2Settings')) {
            console.log('调用原生打开应用设置');
            JsBridge.call('go2Settings', '');
        }
    },
    hasLocationPermission: () => {
        // 1 同意   0 拒绝
        if (JsBridge.hasNativeMethod('hasLocationPermission')) {
            return JsBridge.call('hasLocationPermission');
        }
        return '0'
    },
    checkLocationPermission: () => {
        if (!JsBridge.hasNativeMethod('checkLocationPermission')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('checkLocationPermission', '', (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res);
                } else {
                    reject(res);
                }
            });
        });
    },
    getNavTypes: () => {
        if (!JsBridge.hasNativeMethod('getNavTypes')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('getNavTypes', '', (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res);
                } else {
                    reject(res);
                }
            });
        });
    },
    /**
     * 
     * {
    type:'gd',/gd bd
    lat:'123',
    lng:'32',
     }
     * @returns 
     */
    navigator: (params: any) => {
        if (!JsBridge.hasNativeMethod('navigator')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('navigator', JSON.stringify(params), (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res);
                } else {
                    reject(res);
                }
            });
        });
    },
    scanCode: () => {
        if (!JsBridge.hasNativeMethod('scanCode')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('scanCode', '', (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res)
                } else {
                    reject(res)
                }
            });
        });
    },
    /**
     * {
     * type: 'camera':拍照  'album'
     * }
     * 选择图片/拍照
     * @returns 
     */
    chooseImage: (params: any) => {
        if (!JsBridge.hasNativeMethod('chooseImage')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('chooseImage', JSON.stringify(params), (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res);
                } else {
                    reject(res);
                }
            });
        });
    },
    callTel: (tel: string) => {
        if (JsBridge.hasNativeMethod('callTel')) {
            return JsBridge.call('callTel', tel);
        }
    },
    setItem: (key: string, value: any) => {
        if (JsBridge.hasNativeMethod('setItem')) {
            return JsBridge.call('setItem', JSON.stringify({ key, value }));
        } else {
            if (typeof value === 'object' || value instanceof Object) {
                window.localStorage.setItem(key, JSON.stringify(value));
            } else {
                window.localStorage.setItem(key, value);
            }
        }
    },
    getItem: (key: string) => {
        if (JsBridge.hasNativeMethod('getItem')) {
            const sJson = JsBridge.call('getItem', key);
            try {
                const jsonObj = JSON.parse(sJson);
                return jsonObj ? jsonObj.value : null;
            } catch (e) {
                return null
            }
        } else {
            const result = window.localStorage.getItem(key);
            if (typeof result === 'string') {
                try {
                    const obj = JSON.parse(result);
                    if (typeof obj === 'object' && obj) {
                        return obj;
                    }
                } catch (e) {
                    return result;
                }
            }
            return result;
        }
    },
    clearItem: (key: string) => {
        if (JsBridge.hasNativeMethod('clearItem')) {
            JsBridge.call('clearItem', key);
        } else {
            window.localStorage.removeItem(key);
        }
    },
    clearAllItems: () => {
        if (JsBridge.hasNativeMethod('clearAllItems')) {
            JsBridge.call('clearAllItems', '');
        } else {
            window.localStorage.clear();
        }
    },
    /**
     * {shareUrl:'',shareTitle:'',shareContent:''}
     * @param params 
     * @returns 
     */
    share: (params: any) => {
        if (!JsBridge.hasNativeMethod('share')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('share', JSON.stringify(params), (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res);
                } else {
                    reject(res);
                }
            });
        });
    },
    /**
  * {title:'',url:'',showClose:'1','0'}
  * @param params 
  * @returns 
  */
    loadWebPage: (params: any) => {
        if (JsBridge.hasNativeMethod('loadWebPage')) {
            JsBridge.call('loadWebPage', JSON.stringify(params));
        }
    },
    getAppVersion: () => {
        if (JsBridge.hasNativeMethod('getAppVersion')) {
            return JsBridge.call('getAppVersion');
        }
        return '0.0.0';
    },
    exitApp: () => {
        if (JsBridge.hasNativeMethod('exitApp')) {
            JsBridge.call('exitApp');
        }
    },
    getStatusHeight: () => {
        if (JsBridge.hasNativeMethod('getStatusHeight')) {
            return Number(JsBridge.call('getStatusHeight'));
        }
        return 0;
    },
    getSafeAreaHeight: () => {
        if (JsBridge.hasNativeMethod('getSafeAreaHeight')) {
            return Number(JsBridge.call('getSafeAreaHeight'));
        }
        return 0;
    },
    getDeviceId: () => {
        if (JsBridge.hasNativeMethod('getDeviceId')) {
            return JsBridge.call('getDeviceId');
        }
        return 'unknown';
    },
    openWeChat: () => {
        if (JsBridge.hasNativeMethod('openWeChat')) {
            JsBridge.call('openWeChat');
        }
    },
    /**
     * 跳转到小程序，并返回结果
     * @param params {appletId:'跳转到的小程序的原始id','path':路径,'env':环境 ‘develop preview,publish’}
     * @returns 
     */
    navigateToMiniProgram: (params: any) => {
        if (!JsBridge.hasNativeMethod('navigateToMiniProgram')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('navigateToMiniProgram', JSON.stringify(params), (data: any) => {
                const res = JSON.parse(data);
                if (res.code === 'success') {
                    resolve(res);
                } else {
                    reject(res);
                }
            });
        });
    },
    /**
     * 分享到小程序
     * @param params {appletId:'跳转到的小程序的原始id',url:'https://www.baidu.com',//兼容旧版本，'path':路径,'env':环境 ‘develop preview,publish’,title:'标题’,content:'内容'}
     * @returns 
     */
    shareMiniProgram: (params: any) => {
        if (!JsBridge.hasNativeMethod('shareMiniProgram')) {
            return Promise.resolve({
                code: 'success',
                message: '没有方法'
            });
        }
        return new Promise((resolve, reject) => {
            JsBridge.call('shareMiniProgram', JSON.stringify(params), (data: any) => {
                resolve(JSON.parse(data));
            });
        });
    },
    setH5UrlAndLoad: (url: string) => {
        console.log('setH5UrlAndLoad', url);
        if (JsBridge.hasNativeMethod('setH5UrlAndLoad')) {
            JsBridge.call('setH5UrlAndLoad', url);
        }
    },
};
// #endif

// #ifndef H5 
nativeApi = {}
// #endif
export default nativeApi;